/*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 9:07:00
 * kws
 */

package com.crayon.account.fragments;


import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;

import com.crayon.account.ItemDetailFragment;
import com.crayon.account.dao.DAOAccount;
import com.crayon.account.fragments.ui.FragmentDateMove;
import com.crayon.account.fragments.ui.FragmentSegmentButtom;
import com.crayon.account.fragments.ui.OnSegmentMenuItemCreater;
import com.crayon.account.impl.OnDialogFragment;
import com.crayon.account.impl.OnSegmentRadioChange;
import com.crayon.sharemenu.R;
import com.crayon.util.GDB;
import com.crayon.util.UIBIND;
import com.crayon.util.UTILDate;
import com.crayon.util.UTILSC;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * com.crayon.account.fragments
 * @author kws
 *
 */
public class FRGMonthChart extends ItemDetailFragment implements 
OnClickListener,OnDateSetListener
,OnDialogFragment
,OnSegmentRadioChange
{

	Date mDate;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_account_exlist,container, false);
		mDate =new Date(System.currentTimeMillis());
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		//상단타이틀 세팅
		UIBIND.bindText(getView(), R.id.topTitleName,getString(R.string.title_text_monthly));
		//		View button = getView().findViewById(R.id.buttonTopRight);//.setVisibility(View.GONE);
		UIBIND.bindButton(getView(), R.id.buttonTopRight,getString(R.string.button_text_write));
		UIBIND.bindView(getView(), R.id.buttonTopRight, this);
		
		FragmentTransaction ft = getChildFragmentManager().beginTransaction();
		
		FragmentSegmentButtom fragmentSegmentButtom =new FragmentSegmentButtom();
		fragmentSegmentButtom.setOnSegmentMenuItemCreater(new OnSegmentMenuItemCreater() {
			
			@Override
			public List<SEGMENTMENU> OnBindListItems() {
				// TODO Auto-generated method stub
				return Arrays.asList(
						SEGMENTMENU.SUMMARYIOTOTAL
						,SEGMENTMENU.SUMMARY_CATE
						,SEGMENTMENU.SUMMARYCARDCASH
						,SEGMENTMENU.SUMMARY_IOTYPE
						,SEGMENTMENU.SUMMARY_DAYOFMONTH
						,SEGMENTMENU.SUMMARY_DAYOFWEEK
						);
			}
		});

		fragmentSegmentButtom.setOnSegmentRadioChange(this);
		ft.replace(R.id.fragmentSegmentButtom, fragmentSegmentButtom);
		
		FragmentDateMove fragmentDateMove =new FragmentDateMove();
		ft.replace(R.id.fragmentDateMove, fragmentDateMove);
		
		
		FragmentChildChart childExList = FragmentChildChartIOType.onCrateInstance(fragmentDateMove.getmDate());
		fragmentDateMove.setOnDateChangeListener(childExList);
		ft.replace(R.id.fragment_account_body, childExList);
		ft.commit();

		//class="com.crayon.account.fragments.FragmentSegmentButtom"
		//fragmentSegmentButtom
		
//		FragmentChildExList
//		UIBIND.bindViewVisible(getView(), R.id.buttonTopRight, false);
		//		getView().findViewById(R.id.buttonTopRight);//.setVisibility(View.GONE);
		//		button.setVisibility(View.GONE);
	}


	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.buttonTopRight:
			onWriteCal(null);
			break;
		}
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		// TODO Auto-generated method stub

		mDate = UTILDate.setYYYYMMDD(mDate, year, monthOfYear, dayOfMonth);
		UIBIND.bindButton(getView(), R.id.buttonDateView, UTILDate.getYYYYMM(mDate));

	}
	
	private void onWriteCal(DAOAccount item){

		UTILSC.addOnPopWriteFragment(getActivity(), item, this);
	}

	@Override
	public void onDismissDialog(Fragment fragment) {
		// TODO Auto-generated method stub
		GDB.toast(getActivity(), "popup close");
		Fragment frgbody = getChildFragmentManager().findFragmentById(R.id.fragment_account_body);
		frgbody.onResume();
	}

	@Override
	public void onChangedMenu(SEGMENTMENU menu) {
		// TODO Auto-generated method stub
		FragmentDateMove fragmentDateMove =(FragmentDateMove) getChildFragmentManager().findFragmentById(R.id.fragmentDateMove);
		FragmentTransaction ft = getChildFragmentManager().beginTransaction();
		FragmentChildRoot fragmentChildChart=null;
		if(menu==SEGMENTMENU.SUMMARYIOTOTAL){
			fragmentChildChart = FragmentChildExList.onCrateInstance(fragmentDateMove.getmDate());
			
		}else if(menu==SEGMENTMENU.SUMMARY_IOTYPE){
			fragmentChildChart = FragmentChildChartIOType.onCrateInstance(fragmentDateMove.getmDate());
			
		}else if(menu==SEGMENTMENU.SUMMARYCARDCASH){
			fragmentChildChart = FragmentChildChartCardCash.onCrateInstance(fragmentDateMove.getmDate());
		}else if(menu==SEGMENTMENU.SUMMARY_CATE){
			fragmentChildChart = FragmentChildChartCate.onCrateInstance(fragmentDateMove.getmDate());
			
		}else if(menu==SEGMENTMENU.SUMMARY_DAYOFWEEK){
			fragmentChildChart = FragmentChildChartWeekDays.onCrateInstance(fragmentDateMove.getmDate());
		}else if(menu==SEGMENTMENU.SUMMARY_DAYOFMONTH){
			fragmentChildChart = FragmentChildChartDayOfMonth.onCrateInstance(fragmentDateMove.getmDate());
			
		}else{
			fragmentChildChart = FragmentChildChartIOType.onCrateInstance(fragmentDateMove.getmDate());
			
		}
		fragmentDateMove.setOnDateChangeListener(fragmentChildChart);
		ft.replace(R.id.fragment_account_body, fragmentChildChart);
		ft.commit();

	}
}
